library(data.table)
library(lubridate)
library(stringi)
library(stringr)
library(ggplot2)
library(ggthemes)
library(scales)
library(lfe)
library(stargazer)
# NB: stargazer should be installed with this fix: https://gist.github.com/alexeyknorre/b0780836f4cec04d41a863a683f91b53

# Declare working directory beforehand in an environment variable
# IMPERIAL_LEGAL_POLITICS_REPLICATION_PATH = "path_to_your_folder"
# with the aid of usethis::edit_r_environ()
# Restart R session for the changes to take effect
path <- Sys.getenv("IMPERIAL_LEGAL_POLITICS_REPLICATION_PATH")
setwd(path)

# Load an object with entity-year 2014-2019 panel on participation in litigation
load("data/crimea_krasnodar_orgs_panel.rdata")

##############################
# Data preparation steps

# Create dummies for participation in cases
# (note that we do not count as participation
# cases outside analysis -- i.e. not against government
# or entities not incorporated in Crimea)
crimea_krasnodar_orgs_panel[, used_courts := as.numeric(cases_government_or_local > 0) ]
crimea_krasnodar_orgs_panel[, used_courts_plaintiff := as.numeric(cases_government_or_local_plaintiff > 0) ]
crimea_krasnodar_orgs_panel[, used_courts_respondent := as.numeric(cases_government_or_local_respondent > 0) ]

# Create our region definition
crimea_krasnodar_orgs_panel[region != 'Краснодарский край', region := "Crimea" ]
crimea_krasnodar_orgs_panel[region == 'Краснодарский край', region := "Krasnodar" ]

# Shorter industry names
crimea_krasnodar_orgs_panel[ okved_section_name_eng == "Activities of households as employers; undifferentiated goods-and services-producing activities of households for own use", okved_section_name_eng := "Activities of households as employers"]
crimea_krasnodar_orgs_panel[ okved_section_name_eng == "Water supply; sewerage, waste management, and remediation activities", okved_section_name_eng := "Water supply; sewerage, waste management"]
crimea_krasnodar_orgs_panel[ okved_section_name_eng == "Wholesale and retail trade; repair of motor vehicles and motorcycles", okved_section_name_eng := "Wholesale and retail trade"]
crimea_krasnodar_orgs_panel[ okved_section_name_eng == "Public administration and defence; compulsory social security", okved_section_name_eng := "Public administration and defence"]

# Inverse hyperbolic sine transformation of case counts
crimea_krasnodar_orgs_panel[, cases_government_sinh := log(cases_government + sqrt(cases_government^2 + 1)) ]
crimea_krasnodar_orgs_panel[, cases_local_sinh := log(cases_local + sqrt(cases_local^2 + 1)) ]

# Unique industry identifier
crimea_krasnodar_orgs_panel[, entityid := inn ]
crimea_krasnodar_orgs_panel[is.na(entityid), entityid := ogrn ]

##############################
# Summary statistics

# Use of courts by industry
use_courts_by_industry <- crimea_krasnodar_orgs_panel[!is.na(okved_section_name_eng) & okved_section_name_eng != "Activities of extraterritorial organisations and bodies" & !(type %in% c("regional_company", "municipal_company", "federal_company", "public_service_provider")), list(
		used_courts = round(100*mean(used_courts, na.rm = T), 2),
		used_courts_plaintiff = round(100*mean(used_courts_plaintiff, na.rm = T), 2),
		used_courts_respondent = round(100*mean(used_courts_respondent, na.rm = T), 2)
		),
		by = c("okved_section_name_eng", "region")]
use_courts_by_industry <- dcast(use_courts_by_industry, okved_section_name_eng ~ region, value.var = c("used_courts", "used_courts_plaintiff", "used_courts_respondent"))

# Use of courts by type
use_courts_by_type <- crimea_krasnodar_orgs_panel[!is.na(type) & !(type %in% c("regional_company", "municipal_company", "federal_company", "public_service_provider")), list(
		used_courts = round(100*mean(used_courts, na.rm = T), 2),
		used_courts_plaintiff = round(100*mean(used_courts_plaintiff, na.rm = T), 2),
		used_courts_respondent = round(100*mean(used_courts_respondent, na.rm = T), 2)
		),
		by = c("type", "region")]
use_courts_by_type <- dcast(use_courts_by_type, type ~ region, value.var = c("used_courts", "used_courts_plaintiff", "used_courts_respondent"))

# Use of courts by age
use_courts_by_age <- crimea_krasnodar_orgs_panel[!is.na(age) & !(type %in% c("regional_company", "municipal_company", "federal_company", "public_service_provider")), list(
		used_courts = round(median(ifelse(used_courts == 1, age, NA), na.rm = T), 2),
		used_courts_plaintiff = round(median(ifelse(used_courts_plaintiff == 1, age, NA), na.rm = T), 2),
		used_courts_respondent = round(median(ifelse(used_courts_respondent == 1, age, NA), na.rm = T), 2)
		),
		by = c("region")]
use_courts_by_age[, var := "median age"]		
use_courts_by_age <- dcast(use_courts_by_age, var ~ region, value.var = c("used_courts", "used_courts_plaintiff", "used_courts_respondent"))

# Use of courts by net assets
use_courts_by_netassets <- crimea_krasnodar_orgs_panel[!is.na(net_assets) & !(type %in% c("regional_company", "municipal_company", "federal_company", "public_service_provider")), list(
		used_courts = round(median(ifelse(used_courts == 1, net_assets, NA), na.rm = T), 2),
		used_courts_plaintiff = round(median(ifelse(used_courts_plaintiff == 1, net_assets, NA), na.rm = T), 2),
		used_courts_respondent = round(median(ifelse(used_courts_respondent == 1, net_assets, NA), na.rm = T), 2)
		),
		by = c("region")]
use_courts_by_netassets[, var := "median net assets"]		
use_courts_by_netassets <- dcast(use_courts_by_netassets, var ~ region, value.var = c("used_courts", "used_courts_plaintiff", "used_courts_respondent"))

# Number by region
count_entities_by_region <- crimea_krasnodar_orgs_panel[!is.na(okved_section_name_eng) & okved_section_name_eng != "Activities of extraterritorial organisations and bodies" & !(type %in% c("regional_company", "municipal_company", "federal_company", "public_service_provider")), list(
		entities = uniqueN(entityid)
	),
		by = c("region")]

# Create a grand table
use_courts_table <- rbind(use_courts_by_type, use_courts_by_industry, fill = F, use.names = F)
use_courts_table <- rbind(use_courts_table, use_courts_by_age, fill = F, use.names = F)
use_courts_table <- rbind(use_courts_table, use_courts_by_netassets, fill = F, use.names = F)

##############################
# TABLE S5. Engagement in litigation, Crimea and Sevastopol, or Krasnodar arbitrazh courts in 2014–2019

fwrite(use_courts_table, file = "tables/tableS5_use_courts_table.csv")

##############################
# Case counts regressions

# Create formulas
government_case_counts_formula <- as.formula("cases_government_sinh ~ age + type + as.factor(year) + okved_section_name_eng | 0 | 0 | entityid")
local_case_counts_formula <- as.formula("cases_local_sinh ~ age + type + as.factor(year) + okved_section_name_eng  | 0 | 0 | entityid")

# Fit regressions
fit_government_case_counts_crimea <- felm(government_case_counts_formula, data = crimea_krasnodar_orgs_panel[ region == "Crimea" & !(type %in% c("regional_company", "municipal_company", "federal_company", "public_service_provider")) & okved_section_name_eng != "Activities of extraterritorial organisations and bodies"])

fit_government_case_counts_krasnodar <- felm(government_case_counts_formula, data = crimea_krasnodar_orgs_panel[ region == "Krasnodar" & !(type %in% c("regional_company", "municipal_company", "federal_company", "public_service_provider")) & okved_section_name_eng != "Activities of extraterritorial organisations and bodies"])

fit_local_case_counts_crimea <- felm(local_case_counts_formula, data = crimea_krasnodar_orgs_panel[ region == "Crimea" & !(type %in% c("regional_company", "municipal_company", "federal_company", "public_service_provider")) & okved_section_name_eng != "Activities of extraterritorial organisations and bodies"])

fit_local_case_counts_krasnodar <- felm(local_case_counts_formula, data = crimea_krasnodar_orgs_panel[ region == "Krasnodar" & !(type %in% c("regional_company", "municipal_company", "federal_company", "public_service_provider")) & okved_section_name_eng != "Activities of extraterritorial organisations and bodies"])

##############################
# TABLE S6. Case counts in litigation, Crimea and Sevastopol, or Krasnodar arbitrazh courts in 2014–2019

table_case_counts_government_local <- stargazer(fit_government_case_counts_crimea, fit_government_case_counts_krasnodar, fit_local_case_counts_crimea, fit_local_case_counts_krasnodar, align = F, dep.var.labels = "", column.labels = c("Crimea, gov", "Krasnodar, gov", "Crimea, local", "Krasnodar, local"), column.separate = c(2, 2), no.space = T, keep.stat = c("n", "rsq"), notes = "TBA", type = "latex", style = "ajps")
cat(paste(table_case_counts_government_local, collapse = "\n"), file = "tables/tableS6_case_counts_government_local.tex")
